package com.macro.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.mapper.SpsDataContentMapper;
import com.macro.mall.model.PinionParam;
import com.macro.mall.pojo.*;
import com.macro.mall.query.PinionQuery;
import com.macro.mall.service.SysDashboardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;


@Slf4j
@Service
public class SysDashboardServiceImpl implements SysDashboardService {

    @Autowired
    private SpsDataContentMapper dataContentMapper;


    /***
     * 获取最新的舆情数据
     * @return
     */
    @Override
    public CommonResult newestOpinionData() {
        //Map<String, Long> resultMap = new HashMap<>();
        Map<String, Date> time = getTime();
        Date startDate = time.get("startDate");
        Date endDate = time.get("endDate");
        //查询今日采集量 collection
        OpinionCount opinionCount = dataContentMapper.selectByCollection(startDate, endDate);
        //查询今日舆情量
        Integer opinion = dataContentMapper.selectByOpinion(startDate, endDate);
        opinionCount.setOpinion(opinion);
        return CommonResult.success(opinionCount);
    }


    /***
     * 舆情研判总览
     * @return
     */
    @Override
    public CommonResult getPubOpinionOverview() {
        Map<String, Date> time = getTime();
        Date startDate = time.get("startDate");
        Date endDate = time.get("endDate");
        //查询最新的舆情研判总览
        JudgmentResults results = dataContentMapper.selectNewestOpinionOverview(startDate, endDate);
        results.setTotal(results.getNeutral() + results.getPositive() + results.getNegative());
        return CommonResult.success(results);
    }


    /***
     * 舆情24小时统计
     * @return
     */
    @Override
    public CommonResult opinion24HrsSta() {
        Map<String, Object> mapResult = new HashMap<>();
        List<Opinion24HrsStaVo> opinion24HrsStaVos = dataContentMapper.selectByOpinion24HrsSta();

        log.info("opinion24HrsStaVos", opinion24HrsStaVos);
        SimpleDateFormat sdf = new SimpleDateFormat("dd-HH");
        List<Opinion24HrsSta> hrsStaList = new ArrayList<>();
        for (Opinion24HrsStaVo opinion24HrsStaVo : opinion24HrsStaVos) {
            Opinion24HrsSta hrsSta = new Opinion24HrsSta();
            if (opinion24HrsStaVo.getCreatedDt() != null) {
                hrsSta.setCreatedDt(sdf.format(opinion24HrsStaVo.getCreatedDt()));
            }
            hrsSta.setId(String.valueOf(opinion24HrsStaVo.getId()));
            hrsSta.setJudge(opinion24HrsStaVo.getJudge());
            if (opinion24HrsStaVo.getJudge() != null && opinion24HrsStaVo.getJudge().length() > 0) {
                hrsSta.setJudge(opinion24HrsStaVo.getJudge());
                if (opinion24HrsStaVo.getJudge().equals("2")) {
                    hrsSta.setNeutral(1);
                }
                if (opinion24HrsStaVo.getJudge().equals("3")) {
                    hrsSta.setPositive(1);
                }
                if (opinion24HrsStaVo.getJudge().equals("4")) {
                    hrsSta.setNegative(1);
                }
            }
            hrsStaList.add(hrsSta);
        }


        Map<String, List<Opinion24HrsSta>> map = new HashMap<>();
        for (Opinion24HrsSta opinion24HrsSta : hrsStaList) {
            if (map.get(opinion24HrsSta.getCreatedDt()) == null) {
                List<Opinion24HrsSta> list = new ArrayList();
                list.add(opinion24HrsSta);
                map.put(opinion24HrsSta.getCreatedDt(), list);
            } else {
                List<Opinion24HrsSta> hrsStaList1 = map.get(opinion24HrsSta.getCreatedDt());
                hrsStaList1.add(opinion24HrsSta);

            }
        }
        List<Map<String, Opinion24HrsSta>> result = new ArrayList<>();

        for (Map.Entry<String, List<Opinion24HrsSta>> entry : map.entrySet()) {
            String key = entry.getKey();
            List<Opinion24HrsSta> hrsStaList1 = map.get(key);
            Integer positive = 0;
            Integer negative = 0;
            Integer neutral = 0;
            Integer total = 0;
            Opinion24HrsSta opinion24HrsSta = new Opinion24HrsSta();
            for (Opinion24HrsSta hrsSta : hrsStaList1) {
                if (hrsSta.getPositive() != null) {
                    positive += hrsSta.getPositive();
                    opinion24HrsSta.setPositive(positive);
                    total += hrsSta.getPositive();
                }
                if (hrsSta.getNegative() != null) {
                    negative += hrsSta.getNegative();
                    opinion24HrsSta.setNegative(negative);
                    total += hrsSta.getNegative();
                }
                if (hrsSta.getNeutral() != null) {
                    neutral += hrsSta.getNeutral();
                    opinion24HrsSta.setNeutral(neutral);
                    total += hrsSta.getNeutral();
                }
                if (hrsSta.getCreatedDt() != null) {
                    opinion24HrsSta.setCreatedDt(hrsSta.getCreatedDt());
                }
                opinion24HrsSta.setTotal(total);
            }
            Map<String, Opinion24HrsSta> resultMap = new HashMap<>();
            resultMap.put(key, opinion24HrsSta);
            result.add(resultMap);
        }
        //t2.`name`,
        List<String> list1 = new ArrayList<>();
        List<Opinion24HrsSta> list2 = new ArrayList<>();
        for (Map<String, Opinion24HrsSta> stringOpinion24HrsStaMap : result) {
            Set<Map.Entry<String, Opinion24HrsSta>> entries = stringOpinion24HrsStaMap.entrySet();
            for (Map.Entry<String, Opinion24HrsSta> entry : entries) {
                list1.add(entry.getKey());
                Opinion24HrsSta value = entry.getValue();
                if (value.getNeutral() == null) {
                    value.setNeutral(0);
                }
                if (value.getNegative() == null) {
                    value.setNegative(0);
                }
                if (value.getPositive() == null) {
                    value.setPositive(0);
                }
                if (value.getTotal() == null) {
                    value.setTotal(0);
                }
                list2.add(value);
            }

        }
        mapResult.put("key", list1);
        mapResult.put("value", list2);

        return CommonResult.success(mapResult);
    }


    /***
     * 获取区域统计
     * @return
     */
    @Override
    public CommonResult rgnlStatistics() {
        List<CityOpinion> cityOpinioList = dataContentMapper.selectRgnlStatistics();
        for (CityOpinion cityOpinion : cityOpinioList) {
            if (cityOpinion.getName() != null && cityOpinion.getName().length() > 0) {
                cityOpinion.setName(cityOpinion.getName().substring(0, 2));
            }
        }
        return CommonResult.success(cityOpinioList);
    }


    /***
     * 来源统计
     * @return
     */
    @Override
    public CommonResult srcStatistics() {
        List<CityOpinion> cityOpinioList = dataContentMapper.selectByStatistics();
        return CommonResult.success(cityOpinioList);
    }


    /***
     * 来源统计 折线图
     * @return
     */
    @Override
    public CommonResult srcStatistics2() {
        List<CityOpinion2> cityOpinioList = dataContentMapper.srcStatistics2();
        Map<String, List<CityOpinion2>> resultMap = new HashMap<>();
        for (CityOpinion2 cityOpinion2 : cityOpinioList) {
            if (cityOpinion2.getName() != null) {
                cityOpinion2.setName(cityOpinion2.getName().substring(0, 2));
            }
            if (resultMap.get(cityOpinion2.getName()) == null) {
                List<CityOpinion2> list = new ArrayList<>();
                list.add(cityOpinion2);
                resultMap.put(cityOpinion2.getName(), list);
            } else {
                List<CityOpinion2> list = resultMap.get(cityOpinion2.getName());
                list.add(cityOpinion2);
            }
        }
        return CommonResult.success(resultMap);
    }


    /***
     * 负面舆情
     * @param param
     * @return
     */
    @Override
    public CommonResult negativeOpinion(PinionParam param) {
        Map<String, Object> resultMap = new HashMap<>();
        PageHelper.startPage(param.getPage(), param.getPageSize());
        PinionQuery query = new PinionQuery();
        BeanUtils.copyProperties(param, query);
        List<SpsDataContentVo> contentList =  dataContentMapper.selectNegativeOpinion(query);
        PageInfo<SpsDataContentVo> pageInfo = new PageInfo<>(contentList);
        resultMap.put("total", pageInfo.getTotal());
        resultMap.put("list", pageInfo.getList());
        return CommonResult.success(resultMap);
    }


    private Map<String, Date> getTime() {
        Map<String, Date> resultMap = new HashMap<>();
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_MONTH, -1);//获取当前日期的前一天
        Date startDate = calendar.getTime();
        Calendar cal = Calendar.getInstance();
        Date endDate = cal.getTime();
        resultMap.put("startDate", startDate);
        resultMap.put("endDate", endDate);
        return resultMap;
    }
}
